<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="工单标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入工单标题"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <!-- <el-form-item label="工单编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入工单编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item label="巡检人" prop="inspector">
        <el-select
          v-model="queryParams.inspector"
          placeholder="请输入巡检人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
          filterable
        >
          <el-option
            v-for="item in userList"
            :key="item.id"
            :label="item.nickname"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="巡检日期" prop="inspectDate">
        <el-date-picker
          v-model="queryParams.inspectDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <!-- <el-form-item label="楼栋" prop="building">
        <el-input
          v-model="queryParams.building"
          placeholder="请输入楼栋"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="区域" prop="area">
        <el-input
          v-model="queryParams.area"
          placeholder="请输入区域"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="巡检项" prop="item">
        <el-input
          v-model="queryParams.item"
          placeholder="请输入巡检项"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="异常定位一" prop="location1">
        <el-input
          v-model="queryParams.location1"
          placeholder="请输入异常定位一"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="异常定位二" prop="location2">
        <el-input
          v-model="queryParams.location2"
          placeholder="请输入异常定位二"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="异常描述补充" prop="descriptionExtra">
        <el-input
          v-model="queryParams.descriptionExtra"
          placeholder="请输入异常描述补充"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item label="工单状态" prop="status">
        <el-select v-model="queryParams.status" clearable class="!w-240px">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.PI_BILL_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="处理方法" prop="processMethod">
        <el-select
          v-model="queryParams.processMethod"
          placeholder="请选择处理方法"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="接收人" prop="receiver">
        <el-select
          v-model="queryParams.receiver"
          placeholder="请选择接收人"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="接收日期" prop="receiveDate">
        <el-date-picker
          v-model="queryParams.receiveDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="处理人" prop="processer">
        <el-select
          v-model="queryParams.processer"
          placeholder="请选择处理人"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="处理日期" prop="processDate">
        <el-date-picker
          v-model="queryParams.processDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="派单方式" prop="dispatchMethod">
        <el-select
          v-model="queryParams.dispatchMethod"
          placeholder="请选择派单方式"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="处理结果" prop="processResult">
        <el-select
          v-model="queryParams.processResult"
          placeholder="请选择处理结果"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <!-- <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['pi:workorder:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['pi:workorder:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button> -->
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="工单标题" align="center" prop="title" />
      <el-table-column label="工单编号" align="center" prop="code" />
      <el-table-column label="巡检人" align="center" prop="inspector" v-if="false" />
      <el-table-column label="巡检人" align="center" prop="inspectorName" />
      <el-table-column
        label="巡检日期"
        align="center"
        prop="inspectDate"
        :formatter="dateFormatter2"
        width="180px"
      />
      <el-table-column label="楼栋" align="center" prop="building" v-if="false" />
      <el-table-column label="楼栋" align="center" prop="buildingName" />
      <el-table-column label="区域" align="center" prop="area" v-if="false" />
      <el-table-column label="区域" align="center" prop="areaName" />
      <el-table-column label="巡检项" align="center" prop="item" v-if="false" />
      <el-table-column label="巡检项" align="center" prop="itemName" />
      <el-table-column label="异常定位一" align="center" prop="location1" v-if="false" />
      <el-table-column label="异常定位一" align="center" prop="location1Name" />
      <el-table-column label="异常定位二" align="center" prop="location2" v-if="false" />
      <el-table-column label="异常定位二" align="center" prop="location2Name" />
      <el-table-column label="异常描述" align="center" prop="description" v-if="false" />
      <el-table-column label="异常描述" align="center" prop="descriptionName" width="130px" />
      <el-table-column label="异常描述补充" align="center" prop="descriptionExtra" width="200px" />
      <el-table-column label="异常图片" align="center" prop="fileUrls" width="300px">
        <template #default="scope">
          <el-image
            v-for="(url, index) in scope.row.fileUrls"
            :key="index"
            :src="url"
            :initial-index="index"
            preview-teleported
            :preview-src-list="scope.row.fileUrls"
            :url-list="scope.row.fileUrls"
            show-progress
            class="h-40px max-w-40px mr-0.5"
          />
        </template>
      </el-table-column>
      <el-table-column label="工单状态" align="center" prop="status" width="90px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.PI_BILL_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="处理方法" align="center" prop="processMethod" width="90px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.PI_PROCESS_METHOD" :value="scope.row.processMethod" />
        </template>
      </el-table-column>
      <el-table-column label="接收人" align="center" prop="receiver" v-if="false" />
      <el-table-column label="接收人" align="center" prop="receiverName" />
      <el-table-column
        label="接收日期"
        align="center"
        prop="receiveDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="处理人" align="center" prop="processer" v-if="false" />
      <el-table-column label="处理人" align="center" prop="processerName" />
      <el-table-column
        label="处理日期"
        align="center"
        prop="processDate"
        :formatter="dateFormatter2"
        width="180px"
      />
      <el-table-column label="派单方式" align="center" prop="dispatchMethod">
        <template #default="scope">
          <dict-tag
            :type="DICT_TYPE.PI_DISPATCH_METHOD"
            :value="scope.row.dispatchMethod"
          /> </template
      ></el-table-column>
      <el-table-column label="处理结果" align="center" prop="processResult" v-if="false" />
      <el-table-column label="处理结果" align="center" prop="processResultName" width="120px" />
      <el-table-column label="处理结果图片" align="center" prop="processFileUrls" width="300px">
        <template #default="scope">
          <el-image
            v-for="(url, index) in scope.row.processFileUrls"
            :key="index"
            :src="url"
            :initial-index="index"
            preview-teleported
            :preview-src-list="scope.row.processFileUrls"
            :url-list="scope.row.processFileUrls"
            show-progress
            class="h-40px max-w-40px mr-0.5"
          />
        </template>
      </el-table-column>
      <el-table-column label="处理结果描述" align="center" prop="processDescription" width="300" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <!-- <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['pi:workorder:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['pi:workorder:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <WorkorderForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter,dateFormatter2 } from '@/utils/formatTime'
import download from '@/utils/download'
import { WorkorderApi, WorkorderVO } from '@/api/pi/workorder'
import WorkorderForm from './WorkorderForm.vue'
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { getSimpleUserList } from '@/api/system/user'
/** PI 巡查巡检工单管理 列表 */
defineOptions({ name: 'Workorder' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<WorkorderVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  title: undefined,
  code: undefined,
  inspector: undefined,
  inspectDate: [],
  building: undefined,
  area: undefined,
  item: undefined,
  location1: undefined,
  location2: undefined,
  description: undefined,
  descriptionExtra: undefined,
  fileUrls: undefined,
  status: undefined,
  processMethod: undefined,
  receiver: undefined,
  receiveDate: [],
  processer: undefined,
  processDate: [],
  dispatchMethod: undefined,
  processResult: undefined,
  processFileUrls: undefined,
  processDescription: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const userList = ref<UserVO[]>([])

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await WorkorderApi.getWorkorderPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await WorkorderApi.deleteWorkorder(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await WorkorderApi.exportWorkorder(queryParams)
    download.excel(data, 'PI 巡查巡检工单管理.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(async () => {
  await getList()
  userList.value = await getSimpleUserList()
})
</script>
